package co.yixiang.hw.service.impl;

import co.yixiang.hw.domain.YxStoreCouponProduct;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.hw.repository.YxStoreCouponProductRepository;
import co.yixiang.hw.service.YxStoreCouponProductService;
import co.yixiang.hw.service.dto.YxStoreCouponProductDto;
import co.yixiang.hw.service.dto.YxStoreCouponProductQueryCriteria;
import co.yixiang.hw.service.mapper.YxStoreCouponProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2020-03-10
*/
@Service
//@CacheConfig(cacheNames = "yxStoreCouponProduct")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponProductServiceImpl implements YxStoreCouponProductService {

    @Resource
    private YxStoreCouponProductRepository yxStoreCouponProductRepository;
    @Resource
    private YxStoreCouponProductMapper yxStoreCouponProductMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreCouponProductQueryCriteria criteria, Pageable pageable){
        Page<YxStoreCouponProduct> page = yxStoreCouponProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStoreCouponProductMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxStoreCouponProductDto> queryAll(YxStoreCouponProductQueryCriteria criteria){
        return yxStoreCouponProductMapper.toDto(yxStoreCouponProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreCouponProductDto findById(Integer id) {
        YxStoreCouponProduct yxStoreCouponProduct = yxStoreCouponProductRepository.findById(id).orElseGet(YxStoreCouponProduct::new);
        ValidationUtil.isNull(yxStoreCouponProduct.getId(),"YxStoreCouponProduct","id",id);
        return yxStoreCouponProductMapper.toDto(yxStoreCouponProduct);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCouponProductDto create(YxStoreCouponProduct resources) {
        return yxStoreCouponProductMapper.toDto(yxStoreCouponProductRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCouponProduct resources) {
        YxStoreCouponProduct yxStoreCouponProduct = yxStoreCouponProductRepository.findById(resources.getId()).orElseGet(YxStoreCouponProduct::new);
        ValidationUtil.isNull( yxStoreCouponProduct.getId(),"YxStoreCouponProduct","id",resources.getId());
        yxStoreCouponProduct.copy(resources);
        yxStoreCouponProductRepository.save(yxStoreCouponProduct);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreCouponProductRepository.deleteById(id);
        }
    }

}